/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if(head==nullptr) return nullptr;
        int index = 1;
        ListNode* evenlist = new ListNode(-1);
        auto cur = head;
        auto pre = cur;
        auto tail = evenlist;
        auto next = cur;
        while (cur) {
            if (index % 2 == 0) {
                next = cur->next;
                pre->next = next;
                tail->next = cur;
                tail = tail->next;
                tail->next = nullptr;
                cur = pre;
            }
            pre = cur;
            cur = cur->next;
            index++;
        }
        pre->next = evenlist->next;
        return head;
    }
};
